<?php

namespace App\Http\Controllers\Api;

use App\Http\Resources\ReportResource;
use App\Http\Resources\TechnicianResource;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Models\Technician;
use Illuminate\Support\Facades\Auth;

class TechnicianController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $query = Technician::query()->with(['topic', 'topic.mainFloor', 'topic.mainFloor.detail']);
        if ($request->has('club_id')) {
            $query->where('club_id', $request->club_id);
        }

        $collection = $query->paginate($request->get('per_page', 10));

        $resource = TechnicianResource::collection($collection);

        return api(RET_OK, MSG_OK, $resource);

    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {

    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $user = Auth::user();
        $technician = Technician::query()->withCount(['usersWantAppoint', 'usersHadAppoint'])->findOrFail($id);
        $resource = new TechnicianResource($technician);

        //追加用户是否数据
        $hasWant = $user->wantAppointTechnicians()->wherePivot("target_id", $id)->exists();
        $hasHad = $user->hadAppointTechnicians()->wherePivot("target_id", $id)->exists();
        $resource->additional([
            'has_want' => $hasWant,
            'has_had' => $hasHad,
        ]);

        return api(RET_OK, MSG_OK, $resource);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    public function json(Request $request)
    {
        $out = [];
        $indexed = $request->indexed;
        Technician::query()->get()->each(function ($item) use (&$out, $indexed) {
            $baseData = [
                'id' => $item->id,
                'key' => $item->key,
                'spell' => $item->pinyin,
                'name' => sprintf("%s(%s)", $item->name, $item->short_name),
            ];
            if ($indexed) {
                $letter = substr($item->pinyin, 0, 1);
                $out[strtoupper($letter)][] = $baseData;
                ksort($out);
            } else {
                $out[] = $baseData;
            }
        });

        return api(RET_OK, MSG_OK, $out);

    }
}
